<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="头像">
      </div>
      <!-- 登录表单区域 -->
      <el-form :model="loginForm" ref="loginForm" :rules="LoginFormRules" class="login_form" label-width="0px ">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="iconfont icon-3702mima" @keyup.native.enter="login('loginForm')"
                    v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button :loading="loginProhibit" type="primary" @click="login('loginForm')">登录</el-button>
          <el-button type="info" @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import common from '@/assets/js/common'
export default {
  data () {
    return {
      loginProhibit: false,
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      LoginFormRules: {
        username: this.config.rules.usernameRules,
        password: this.config.rules.passwordRules
      }
    }
  },
  methods: {
    async login (formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          return this.$message.error('请正确填写表单')
        }
        await common.preventDuplicateSubmit(loginProhibit => {
          this.loginProhibit = loginProhibit
        }, async () => {
          const { data: res } = await this.http.post('login', this.loginForm)
          if (res.meta.status !== 200) {
            return this.$message.error(`${res.meta.status} ${res.meta.msg}`)
          }
          this.$message.success(`${res.meta.status} ${res.meta.msg}`)

          // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
          //  1.1 项目中其他登录之外的接口，必须在登录之后才能访问
          //  1.2 token 只应在当前网站打开期间有效，所以将token保存在 sessionStorage 中
          this.config.authorization.token = res.data.token
          // 2.通过编程式导航跳转到后台主页，路由地址是 /home
          await this.$router.push('/home')
        })
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  margin: 0;
  padding: 0;
  background-color: #2b4b6b;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login_box {
  position: relative;
  //transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  //left: 50%;
  //top: 50%;

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ccc;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;

    .btns {
      display: flex;
      justify-content: right;
    }
  }
}
</style>
